//
//  CZUserAccount.swift
//  weibo27
//
//  Created by mac on 16/4/17.
//  Copyright © 2016年 it.cast. All rights reserved.
//

import UIKit

class CZUserAccount: NSObject ,NSCoding {
    ///access_token 令牌
    var access_token:String?
    ///  access_token的生命周期，单位是秒数。
    var expires_in:NSTimeInterval = 0{
        didSet{
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    var remind_in:String?
    /// 授权用户的UID
    var uid:String?
    
    /// 过期日期的属性 开发者的有效期是五年
    var expiresDate:NSDate?
    
    /// 用户昵称
    var screen_name: String?
    /// 用户头像地址（大图），180×180像素
    var avatar_large:String?
    
    
    //构造函数
    init(dict: [String: AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    //为没有赋值的属性进行忽略操作
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
        
    }
    
    //重写它的description
    override var description: String{
        
        let keys = ["access_token","expires_in","remind_in","uid","expiresDate","screen_name","avatar_large"]
        
        return "\(dictionaryWithValuesForKeys(keys))"
    }
    
    
    // MARK: 归档与解档
    //就是将对象转换为二进制的数存储到磁盘文件中
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
    }
    
    //就是将磁盘中的文件转换成对象
    required init?(coder aDecoder: NSCoder){
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
        
    }
}



